﻿using System.Windows;
using System.Windows.Controls;
using System.Configuration;
using System.Collections.Generic;
using TestStarter.Wpf;
using TestStater.Wpf.WpfWindow;
using System.Collections.ObjectModel;
using System;
using Microsoft.Win32;

namespace TestStarter.Wpf.WpfPage
{
    /// <summary>
    /// Interaction logic for SettingsPage.xaml
    /// </summary>
    public partial class SettingsPage : Page
    {
        public IList<string> ErrorList {get; set;}
        public ObservableCollection<Browser> BrowserList { get; set; }

        public SettingsPage()
        {
            InitializeComponent();
            ErrorList = new List<string>();
            SetDefaultValues(); 
        }

        public SettingsPage(IList<string> errorList)
        {
            InitializeComponent();
            ErrorList = errorList;
            SetDefaultValues();
        }

        private void SetDefaultValues()
        {
            textBoxScanningFolder.Text = TestStarter.Util.ApplicationSettings.SCAN_FOLDER;
            textBoxServerUrl.Text = TestStarter.Util.ApplicationSettings.SERVER_URL;

            SetBrowsersList();
            BrowserslistBox.ItemsSource = BrowserList;
            BrowserslistBox.Items.Refresh();

        }

        public void SetBrowsersList()
        {
            RegistryKey browserKeys;
            string keypath = @"SOFTWARE\WOW6432Node\Clients\StartMenuInternet";

            browserKeys = Registry.LocalMachine.OpenSubKey(keypath);
            if (browserKeys == null)
            {
                keypath= @"SOFTWARE\Clients\StartMenuInternet";
                browserKeys = Registry.LocalMachine.OpenSubKey(keypath);
            }
           
            string[] browserNames = browserKeys.GetSubKeyNames();

            BrowserList = new ObservableCollection<Browser>();
            int index = 0;

            foreach(string str in browserNames)
            {
                RegistryKey reg = Registry.LocalMachine.OpenSubKey(keypath+"\\"+str);
                string strVal = reg.GetValue(null).ToString();

                if(strVal.Contains("Internet Explorer"))
                {
                    strVal = strVal + GetIEVersion().ToString();
                }

                BrowserList.Add(new Browser { BrowserName = strVal, Key = index });
            }
        }



        public static int GetIEVersion()
        {
            // string strKeyPath = @"HKLM\SOFTWARE\Microsoft\Internet Explorer";
            string strKeyPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer";
            string[] ls = new string[] { "svcVersion", "svcUpdateVersion", "Version", "W2kVersion" };

            int maxVer = 0;
            for (int i = 0; i < ls.Length; ++i)
            {
                object objVal = Microsoft.Win32.Registry.GetValue(strKeyPath, ls[i], "0");
                string strVal = System.Convert.ToString(objVal);
                if (strVal != null)
                {
                    int iPos = strVal.IndexOf('.');
                    if (iPos > 0)
                        strVal = strVal.Substring(0, iPos);

                    int res = 0;
                    if (int.TryParse(strVal, out res))
                        maxVer = Math.Max(maxVer, res);
                } // End if (strVal != null)

            } // Next i

            return maxVer;
        } // End Function GetBrowserVersion 

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            settingStatus.Content = "";
            string url = textBoxServerUrl.Text;
            string folderPath = textBoxScanningFolder.Text;
            ErrorList = TestStarter.Util.ApplicationSettings.UpdateValues(url, folderPath);
            if (!ErrorList.Equals(null))
            {
                string errorString = "";
                foreach (string value in ErrorList)
                {
                    errorString = errorString + "\n" + value;
                }

               
                if(errorString.Equals(""))
                {
                    settingStatus.Content = "Settings saved.";
                }
                else
                {
                    MSGWindow error = new MSGWindow("Error !!!", errorString);
                    error.Show();
                }
            }
           
        }

        private void ScanFolderBrowseButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
            
            if (textBoxScanningFolder.Text != null || textBoxScanningFolder.Text.Trim().Length != 0)
                folderDialog.SelectedPath = textBoxScanningFolder.Text;
            else
                folderDialog.SelectedPath = "C:\\";
            

           System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();
           if (result == System.Windows.Forms.DialogResult.OK)
                textBoxScanningFolder.Text = folderDialog.SelectedPath;
        }


        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            
        }
    }

    public class Browser
    {
        public string BrowserName { get; set; }
        public int Key { get; set; }
    }
}
